home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 363 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  2. Message-ID: <DMIMKo.B6o@uns.bris.ac.uk>
  3. X-Original-Date: Fri, 9 Feb 1996 15:48:24 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 10 Feb 96 05:01:38 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Nesting scope resolution
  9. Organization: Inmos
  10. X-Newsreader: TIN [version 1.2 PL2]
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMRwm1uEDnX0m9pzZAQEeEQF6AlwOKh3yx8p7Eph/IylC5VX/vP+n6+bM
  13.     z/6MnGl4VzY6Y2Q1I29XWX3ar/qqDXry
  14.     =fsUG
  15.  
  16. Is the line tagged //B legal?
  17.  
  18. class One
  19. {     
  20.   unsigned  scalar;
  21. public:
  22.   void func(int);
  23. };
  24. class Two : public One
  25. {
  26. protected:
  27.   typedef One Parent;
  28. };
  29. class Three : public Two
  30. {
  31. protected:
  32.   typedef Two Parent;
  33. public:
  34.   void func(int);
  35. };          
  36.       
  37. void Three::func(int i)
  38. {
  39.   One::func(i);                 //A
  40.   Parent::Parent::func(i);      //B
  41. }
  42.  
  43. I believe lines A and B are equivalent, but some compilers say yes and some
  44. say no. Visual C++, Borland C++ and cfront reject it. G++ and
  45. SGI C++ accept it.
  46.  
  47. The construction Parent::Parent::func seems a natural way of getting
  48. at the grandparent methods, without actually knowing the name of the
  49. grandparent class.
  50.  
  51. The draft ANSI standard has this to say
  52.  
  53. 5.1 defines primary expressions, one of which is an id-expression,
  54.  
  55. id-expression:
  56.     unqualified-id
  57.     qualified-id
  58.  
  59. qualified-id:
  60.     nested-name-specifier template<opt> unqualified-id
  61.  
  62. unqualified-id:
  63.     identifier
  64.     ...
  65.  
  66. a function call is a postfix expression (5.2), which is
  67.  
  68. postfix-expression:
  69.     primary-expression
  70.     postfix-expression(expression-list<opt>)
  71.     ...
  72.  
  73. the nested-name-specified is defined in 7.3.1.1
  74.  
  75. nested-name-specifier:
  76.     class-or-namespace-name :: nested-name-specifier<opt>
  77.  
  78. class-or-namespace-name:
  79.     class-name
  80.     namespace-name
  81.  
  82. it also adds (para 3) that 'The search of a name after a :: locates only named
  83. members of a namespace or class.'
  84.  
  85. So the expression 'Parent::Parent::func(i)' looks like a
  86. postfix-expression constructed of 
  87. primary-expression['Parent::Parent::func'] ( expression-list['i'] )
  88.  
  89. the primary-expression 'Parent::Parent::func'
  90. looks like a qualified-id of
  91. nested-name-specifier['Parent::Parent::'] unqualified-id[func]
  92.  
  93. and the nested-name-specifier 'Parent::Parent::' is
  94. class-or-namespace-name['Parent'] :: nested-name-specifier['Parent::']
  95.  
  96. of which the nested-name-specifier is again
  97. class-or-namespace-name['Parent'] ::
  98.  
  99. now the question is, is Parent (a typedef) considered a class-name?
  100.  
  101. section 7.1.3 has this to say about typedefs. 'A typedef-name is thus
  102. a synonym for another type'. Classes are definatly types, so Parent is
  103. a synonym for a class. Para 3 of 7.3.1.1 tells us which typedef to use
  104. after the respective ::. And of course, if Parent wasn't considered a
  105. class-name, I wouldn't be able to write the simple Parent::func, to
  106. get a superclass's method (and we know that works).
  107.  
  108. QED
  109.  
  110. Another way of looking at it is that inside a class member function,
  111. member names have an implicit '<ClassName>::' prepended to them.
  112. so writing Parent::func() (which is definiatly ok) is shorthand for
  113. Three::Parent::func(), which isn't a million miles from what I've actually
  114. written.
  115.  
  116. So which is right?
  117.  
  118. nathan
  119.  
  120. --
  121. Nathan Sidwell                         Holder of the Xmris home page
  122. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  123. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  124. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  125. ---
  126. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  127.   Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  128.   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  129.